/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange;

import org.crystalrange.service.MenuFolderService;
import org.crystalrange.service.MenuItemService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;

public class MenuController extends BaseController {

    private Tree treeMenu;
    private MenuFolderService menuFolderService;
    private MenuItemService menuItemService;

    public MenuFolderService getMenuFolderService() {
        return this.menuFolderService;
    }

    public void setMenuFolderService(MenuFolderService menuFolderService) {
        this.menuFolderService = menuFolderService;
    }

    public MenuItemService getMenuItemService() {
        return this.menuItemService;
    }

    public void setMenuItemService(MenuItemService menuItemService) {
        this.menuItemService = menuItemService;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        MenuRootNode menuRootNode = new MenuRootNode();
        menuRootNode.setMenuFolderService(this.menuFolderService);
        menuRootNode.setMenuItemService(this.menuItemService);
        menuRootNode.readChildren();

        this.treeMenu.setModel(new MenuTreeModel(menuRootNode));
        this.treeMenu.setItemRenderer(new MenuTreeRenderer());
    }

    public void onClick$menuitemLogout(Event event) throws Exception {
        this.logout();
    }
    
    public void onClick$menuitemHelpAbout(Event event) throws Exception {    
        //Messagebox.show(SecurityContextHolder.getContext().getAuthentication().getName());
    }

    public void onClick$menuitemHelpContents(Event event) throws Exception {
        Clients.evalJavaScript("window.open('" + this.execution.encodeURL("~./zul/html/helpContent.zul") + "', '_blank');");
    }

    public void onSelect$treeMenu(SelectEvent event) throws Exception {
        Treeitem treeitem = this.treeMenu.getSelectedItem();
        if (treeitem.getValue() instanceof MenuItemNode) {
            MenuItemNode menuItemNode = (MenuItemNode) treeitem.getValue();
            this.execution.sendRedirect(menuItemNode.getLocation());
        }
    }

    private void logout() throws Exception {
        this.execution.sendRedirect("/j_spring_security_logout");
    }
}
